﻿#region DaEquip
/*----------------------------------------------------------------
// 文件名：DaEquip.cs
// 功能描述：表dbo.Equip的数据访问层
//
// 
// 创建时间：2009-11-20 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,IEquip ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaEquip : DaBase<MoEquip>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " EQUIP_ID ,EQUIP_NAME ,OEM ,EX_FACT_DATE ,NAMEPLATE ,BUY_DATE ,USEFUL_LIFE ,MAINTAIN_DAY_CYC ,EQUIP_CONFIGS   ";

        internal const string SQL_INSERT = "INSERT INTO EQUIP (" + TABLE_COLUMNS + ") VALUES (@EquipId,@EquipName,@Oem,@ExFactDate,@Nameplate,@BuyDate,@UsefulLife,@MaintainDayCyc,@EquipConfigs)";
        internal const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM EQUIP  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE EQUIP_ID=@EquipId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM EQUIP  WHERE EQUIP_ID=@EquipId  ";
        internal const string SQL_UPDATE = "UPDATE EQUIP SET EQUIP_NAME=@EquipName, OEM=@Oem, EX_FACT_DATE=@ExFactDate, NAMEPLATE=@Nameplate, BUY_DATE=@BuyDate, USEFUL_LIFE=@UsefulLife, MAINTAIN_DAY_CYC=@MaintainDayCyc, EQUIP_CONFIGS=@EquipConfigs WHERE EQUIP_ID=@EquipId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM EQUIP ";
        internal const string SQL_DELETE = "DELETE FROM EQUIP WHERE EQUIP_ID=@EquipId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM Equip ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaEquip()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaEquip(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaEquip(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoEquip ConstructT()
        {
            return new MoEquip();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "EquipName", DbType.String);
            db.AddInParameter(dbCommand, "Oem", DbType.String);
            db.AddInParameter(dbCommand, "ExFactDate", DbType.DateTime);
            db.AddInParameter(dbCommand, "Nameplate", DbType.AnsiString);
            db.AddInParameter(dbCommand, "BuyDate", DbType.DateTime);
            db.AddInParameter(dbCommand, "UsefulLife", DbType.Decimal);
            db.AddInParameter(dbCommand, "MaintainDayCyc", DbType.Int32);
            db.AddInParameter(dbCommand, "EquipConfigs", DbType.String);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoEquip entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString, entity.EquipId);
            db.AddInParameter(dbCommand, "EquipName", DbType.String, entity.EquipName);
            db.AddInParameter(dbCommand, "Oem", DbType.String, entity.Oem);
            db.AddInParameter(dbCommand, "ExFactDate", DbType.DateTime, entity.ExFactDate);
            db.AddInParameter(dbCommand, "Nameplate", DbType.AnsiString, entity.Nameplate);
            db.AddInParameter(dbCommand, "BuyDate", DbType.DateTime, entity.BuyDate);
            db.AddInParameter(dbCommand, "UsefulLife", DbType.Decimal, entity.UsefulLife);
            db.AddInParameter(dbCommand, "MaintainDayCyc", DbType.Int32, entity.MaintainDayCyc);
            db.AddInParameter(dbCommand, "EquipConfigs", DbType.String, entity.EquipConfigs);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoEquip entity)
        {
            db.SetParameterValue(addCmd, "EquipId", entity.EquipId);
            db.SetParameterValue(addCmd, "EquipName", entity.EquipName);
            db.SetParameterValue(addCmd, "Oem", entity.Oem);
            db.SetParameterValue(addCmd, "ExFactDate", entity.ExFactDate);
            db.SetParameterValue(addCmd, "Nameplate", entity.Nameplate);
            db.SetParameterValue(addCmd, "BuyDate", entity.BuyDate);
            db.SetParameterValue(addCmd, "UsefulLife", entity.UsefulLife);
            db.SetParameterValue(addCmd, "MaintainDayCyc", entity.MaintainDayCyc);
            db.SetParameterValue(addCmd, "EquipConfigs", entity.EquipConfigs);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "EquipId", row["EquipId"]);
            db.SetParameterValue(addCmd, "EquipName", row["EquipName"]);
            db.SetParameterValue(addCmd, "Oem", row["Oem"]);
            db.SetParameterValue(addCmd, "ExFactDate", row["ExFactDate"]);
            db.SetParameterValue(addCmd, "Nameplate", row["Nameplate"]);
            db.SetParameterValue(addCmd, "BuyDate", row["BuyDate"]);
            db.SetParameterValue(addCmd, "UsefulLife", row["UsefulLife"]);
            db.SetParameterValue(addCmd, "MaintainDayCyc", row["MaintainDayCyc"]);
            db.SetParameterValue(addCmd, "EquipConfigs", row["EquipConfigs"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "EquipName", DbType.String);
            db.AddInParameter(dbCommand, "Oem", DbType.String);
            db.AddInParameter(dbCommand, "ExFactDate", DbType.DateTime);
            db.AddInParameter(dbCommand, "Nameplate", DbType.AnsiString);
            db.AddInParameter(dbCommand, "BuyDate", DbType.DateTime);
            db.AddInParameter(dbCommand, "UsefulLife", DbType.Decimal);
            db.AddInParameter(dbCommand, "MaintainDayCyc", DbType.Int32);
            db.AddInParameter(dbCommand, "EquipConfigs", DbType.String);
            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoEquip entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "EquipName", DbType.String, entity.EquipName);
            db.AddInParameter(dbCommand, "Oem", DbType.String, entity.Oem);
            db.AddInParameter(dbCommand, "ExFactDate", DbType.DateTime, entity.ExFactDate);
            db.AddInParameter(dbCommand, "Nameplate", DbType.AnsiString, entity.Nameplate);
            db.AddInParameter(dbCommand, "BuyDate", DbType.DateTime, entity.BuyDate);
            db.AddInParameter(dbCommand, "UsefulLife", DbType.Decimal, entity.UsefulLife);
            db.AddInParameter(dbCommand, "MaintainDayCyc", DbType.Int32, entity.MaintainDayCyc);
            db.AddInParameter(dbCommand, "EquipConfigs", DbType.String, entity.EquipConfigs);
            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString, entity.EquipId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoEquip entity)
        {
            db.SetParameterValue(updateCmd, "EquipName", entity.EquipName);
            db.SetParameterValue(updateCmd, "Oem", entity.Oem);
            db.SetParameterValue(updateCmd, "ExFactDate", entity.ExFactDate);
            db.SetParameterValue(updateCmd, "Nameplate", entity.Nameplate);
            db.SetParameterValue(updateCmd, "BuyDate", entity.BuyDate);
            db.SetParameterValue(updateCmd, "UsefulLife", entity.UsefulLife);
            db.SetParameterValue(updateCmd, "MaintainDayCyc", entity.MaintainDayCyc);
            db.SetParameterValue(updateCmd, "EquipConfigs", entity.EquipConfigs);
            db.SetParameterValue(updateCmd, "EquipId", entity.EquipId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "EquipName", row["EquipName"]);
            db.SetParameterValue(updateCmd, "Oem", row["Oem"]);
            db.SetParameterValue(updateCmd, "ExFactDate", row["ExFactDate"]);
            db.SetParameterValue(updateCmd, "Nameplate", row["Nameplate"]);
            db.SetParameterValue(updateCmd, "BuyDate", row["BuyDate"]);
            db.SetParameterValue(updateCmd, "UsefulLife", row["UsefulLife"]);
            db.SetParameterValue(updateCmd, "MaintainDayCyc", row["MaintainDayCyc"]);
            db.SetParameterValue(updateCmd, "EquipConfigs", row["EquipConfigs"]);
            db.SetParameterValue(updateCmd, "EquipId", row["EquipId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "EquipId", DbType.AnsiString, "EQUIP_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "EquipName", DbType.String, "EQUIP_NAME", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Oem", DbType.String, "OEM", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "ExFactDate", DbType.DateTime, "EX_FACT_DATE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Nameplate", DbType.AnsiString, "NAMEPLATE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "BuyDate", DbType.DateTime, "BUY_DATE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "UsefulLife", DbType.Decimal, "USEFUL_LIFE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "MaintainDayCyc", DbType.Int32, "MAINTAIN_DAY_CYC", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "EquipConfigs", DbType.String, "EQUIP_CONFIGS", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            db.AddInParameter(dbUpdateCommand, "EquipName", DbType.String, "EQUIP_NAME", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Oem", DbType.String, "OEM", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "ExFactDate", DbType.DateTime, "EX_FACT_DATE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Nameplate", DbType.AnsiString, "NAMEPLATE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "BuyDate", DbType.DateTime, "BUY_DATE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "UsefulLife", DbType.Decimal, "USEFUL_LIFE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "MaintainDayCyc", DbType.Int32, "MAINTAIN_DAY_CYC", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "EquipConfigs", DbType.String, "EQUIP_CONFIGS", DataRowVersion.Current);
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "EquipId", DbType.AnsiString, "EQUIP_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "EquipId", DbType.AnsiString, "EQUIP_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoEquip entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString, entity.EquipId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoEquip entity)
        {
            db.SetParameterValue(deleteCmd, "EquipId", entity.EquipId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "EquipId", row["EquipId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoEquip entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString, entity.EquipId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoEquip entity)
        {
            if (!reader.IsDBNull(0)) entity.EquipId = reader.GetString(0);
            if (!reader.IsDBNull(1)) entity.EquipName = reader.GetString(1);
            if (!reader.IsDBNull(2)) entity.Oem = reader.GetString(2);
            if (!reader.IsDBNull(3)) entity.ExFactDate = reader.GetDateTime(3);
            if (!reader.IsDBNull(4)) entity.Nameplate = reader.GetString(4);
            if (!reader.IsDBNull(5)) entity.BuyDate = reader.GetDateTime(5);
            if (!reader.IsDBNull(6)) entity.UsefulLife = reader.GetDecimal(6);
            if (!reader.IsDBNull(7)) entity.MaintainDayCyc = reader.GetInt32(7);
            if (!reader.IsDBNull(8)) entity.EquipConfigs = reader.GetString(8);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoEquip entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString, entity.EquipId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

